package history.study.array;

// 69. x 的平方根:https://leetcode-cn.com/problems/sqrtx/
public class LeetCode_69 {
    public static void main(String[] args) {
        System.out.println(mySqrt(2147395599));
    }

    // 二分查找求解
    public static int mySqrt(int x) {
        if (x <=0) return 0;
        long left = 1, right = x;
        long ret = left;
        while (left<=right) {
            long mid = left+((right-left)>>1);
            long midResult = mid*mid;
            if (midResult > x) {
                right = mid-1;
            } else if (midResult < x) {
                ret = mid;
                left = mid+1;
            } else {
                return (int)mid;
            }
        }
        return (int)ret;
    }
}
